* CLEANING AND VARIABLE CREATION: SECOND FOLLOW-UP DATA

*********************************************************************************

use "${data}/r2_followup_raw.dta", clear // load second follow-up

* Rename
ren A2 hh_id
lab var hh_id "household id"
ren a14a gpname
lab var gpname "village name"
ren a14b gp_code
lab var gp_code "village id"
ren a14d tokname
lab var tokname "tok name"
ren a14c uniquegrp
lab var uniquegrp "tok code"
ren a15 headhhname
lab var headhhname "name of household head"
order hh_id gpname-headhhname, first
ren a6 IDvisit1
lab var IDvisit1 "Enumerator ID"
ren a92 IDcompleted
lab var IDcompleted "Enumerator ID at completed visit"
ren a102 IDsupervisor
lab var IDsupervisor "Supervisor ID"

* Renaming "cough/cold in the last two weeks" variable
ren d2_8 d2_01
ren d2_2 d2_02
ren d2_3 d2_03
ren d2_4 d2_04
ren d2_5 d2_05
ren d2_6 d2_06
ren d1_7 d2_07
ren QT d2_08
ren d2_9 d2_09
ren d2_80 d2_10
ren d2_81 d2_11
ren d2_82 d2_12
ren d2_83 d2_13
ren d2_84 d2_14
ren d2_85 d2_15

* Address households missing/not surveyed in second follow-up
clonevar hhmissing = a7
clonevar a7_1 = a71
lab var hhmissing "Whether household was surveyed, refused, missing etc"
replace hhmissing = 5 if a7_1 == "They had left the village"
replace hhmissing = 1 if hhmissing == . & a91 != ""
replace hhmissing = 8 if hhmissing == . 
gen lostin2014 = 0
replace lostin2014 = 1 if hhmissing != 1
lab var lostin2014 "Household was lost to study in 2014, but had been previously interviewed" 

* General cleaning and variable creation

local household_members "01 02 03 04 05 06 07 08 09 10 11 12 13 14 15"

foreach i in `household_members' {

	* Household members (for household size)
	clonevar hh_member_`i' = c2_`i' 
	replace hh_member_`i' = trim(hh_member_`i')
	tostring hh_member_`i', replace
	
	* Age of household members (for number of children under five and age of household head)
	clonevar age_year_`i' = c51_`i'
	clonevar age_month_`i' = c52_`i'
	clonevar age_day_`i' = c53_`i'
	
	* Household-head indicator
	gen household_head_`i' = 0
	replace household_head_`i' = c61_`i' if c61_`i' == 1
	
	* Sex of household members (for female head of household)
	clonevar sex_`i' = c4_`i'
	
	* Primary-cook indicator
	clonevar cooks_`i' = c8_`i'
	capture replace cooks_`i' = "1" if cooks_`i' == "1,2"
	destring cooks_`i', replace
	replace cooks_`i' = 2 if cooks_`i' > 2 & !mi(cooks_`i')
	gen primary_cook_`i' = 0
	replace primary_cook_`i' = 1 if cooks_`i' == 1
		
	* Cough-cold indicator
	clonevar cough_or_cold_`i' = d2_`i'
	capture replace cough_or_cold_`i' = "-9" if cough_or_cold_`i' == "(-9)"
	capture replace cough_or_cold_`i' = "1" if cough_or_cold_`i' == "0,1"
	destring cough_or_cold_`i', replace
	}

* Stove-ownership and stove-use variables
ren e9an1_81an e9an1_11an 
ren e9an1_81bn e9an1_11bn 
ren e9an1_82 e9an1_12
ren e9an2_81an e9an2_11an 
ren e9an2_81bn e9an2_11bn 
ren e9an2_82 e9an2_12
label var e1_01 "=1 if hh owns mitti ka chulha"
label var e1_01an2 "=1 if hh owns anjeti stove"
label var e1_03 "=1 if hh owns three stone fire stove"
label var e1_04 "=1 if hh owns kerosene stove"
label var e1_04bn2 "=1 if hh owns kerosene heater"
label var e1_06 "=1 if hh owns LPG stove"
label var e1_07an2 "=1 if hh owns electric stove, can hold vessels"
label var e1_07bn "=1 if hh owns g-coil stove"
label var e1_07cn2 "=1 if hh owns electric coil heater, no vessels"
label var e1_07dn2 "=1 if hh owns electric halogen heater"
label var e1_07en2 "=1 if hh owns electric heater - air blower"
label var e1_08 "=1 if hh owns biogas stove"
label var e1_09 "=1 if hh owns sagarh"
label var e1_11an "=1 if hh owns greenway"
label var e1_11bn "=1 if hh owns other ICS"
label var e1_12 "=1 if hh owns other stove"
foreach i in 01 01an2 03 04 04bn2 06 07an2 07bn 07cn2 07dn2 07en2 08 09 11an 11bn 12 {
	clonevar stove_own_`i' = e1_`i'
	clonevar number_stove_own_`i' = e2_`i'
	clonevar stove_used_`i' = e6_`i'
	clonevar stove_use_hours_`i' = e9an1_`i'
	clonevar stove_use_minutes_`i' = e9an2_`i'
	}
foreach var of varlist stove_used_* {
	capture replace `var' = "1" if `var' == "0, 1"
	destring `var', replace
	replace `var' = 1 if `var' > 1 & !mi(`var')
}
	
* Fuel-use indicator, fuel-use regularity, fuel-collection time, and fuel-expenditure variables
foreach var in e18 e20 e24a e24b e21na e21nb {
	ren `var'_8 `var'_01
	ren `var'_2 `var'_02
	ren `var'_3 `var'_03
	ren `var'_4 `var'_04
	ren `var'_5 `var'_05
	ren `var'_6 `var'_06
	ren `var'_7 `var'_07
	ren `var'_9 `var'_09
	}
ren BFY e18_08
ren BGB e20_08
ren BGI e24a_08
ren BGJ e24b_08
ren BGD e21na_08
ren BGE e21nb_08
lab var e18_01 "Use fuel: fuelwood"
lab var e18_02 "Use fuel: crop residue"
lab var e18_03 "Use fuel: leaves"
lab var e18_04 "Use fuel: dung"
lab var e18_05 "Use fuel: biomas pellets"
lab var e18_06 "Use fuel: kerosene"
lab var e18_07 "Use fuel: LPG"
lab var e18_08 "Use fuel: electricity"
lab var e18_09 "Use fuel: biogas"
lab var e18_10 "Use fuel: other"
lab var e18_11n2 "Use fuel: trash"
foreach i in 01 02 03 04 05 06 07 08 09 10 11n2 {

	* Fuel use
	clonevar fuel_use_`i' = e18_`i'
	capture replace fuel_use_`i' = "1" if fuel_use_`i' == "0,1"
	capture replace fuel_use_`i' = "1" if fuel_use_`i' == "0, 1"
	destring fuel_use_`i', replace
	clonevar fuel_use_regularity_`i' = e20_`i'
	
	* Fuel collection time
	clonevar fuel_collection_time_`i' = e24a_`i'
	clonevar fuel_collection_units_`i' = e24b_`i'
	* Recording fuel-collection time units as per responses.
	* NB: Time-unit responses are restricted in survey instrument but appear to have been given nonetheless.
	capture replace fuel_collection_units_`i' = "1" if fuel_collection_units_`i' == "Every Day" | fuel_collection_units_`i' == "Per Day" | fuel_collection_units_`i' == "Per day" 
	capture replace fuel_collection_units_`i' = "2" if fuel_collection_units_`i' == "Per Week" | fuel_collection_units_`i' == "Per week" | fuel_collection_units_`i' == "Every Week" | fuel_collection_units_`i' == "per week"
	capture replace fuel_collection_units_`i' = "3" if fuel_collection_units_`i' == "Monthly" | fuel_collection_units_`i' == "Per Month"
	destring fuel_collection_units_`i', replace
	
	* Fuel regularity
	clonevar fuel_expenditure_`i' = e21na_`i'
	clonevar fuel_expenditure_units_`i' = e21nb_`i'
	
	}
* Recoding fuel-collection time variables coded as string with fractions (with slashes)
foreach var of varlist fuel_collection_time_* {
	capture confirm string var `var'
	if _rc == 0 {
		destring `var', gen(temp`var') force 
		gen `var'num = trim(substr(`var', 1, strpos(`var', "/") - 1))
		gen `var'denom = trim(substr(`var', strpos(`var', "/") + 1, 10))
		replace `var'denom = "" if `var'num == ""
		destring `var'num, replace
		destring `var'denom, replace
		gen `var'frac = `var'num / `var'denom
		replace temp`var' = `var'frac if temp`var' == . & `var'frac != .
		order temp`var', a(`var')
		rename `var' string_`var'
		rename temp`var' `var'
		drop `var'num `var'denom string_`var' `var'frac
		}
	else disp "`var' is already numeric"
	}
* Specifying fuel-collection time units per the follow-up survey instrument
forval i = 1/11 {
	if `i' < 10 {	
		gen fuel_collection_minutes_0`i' = .
		if (`i' <= 5) | (`i' > 7) {
			replace fuel_collection_minutes_0`i' = (fuel_collection_time_0`i' / 7) * 60 // Hours per week converted to minutes per day
			}		
		else if (`i' == 6) | (`i' == 7) {
			replace fuel_collection_minutes_0`i' = (fuel_collection_time_0`i' / 30) * 60 // Hours per month converted to minutes per day
			}		
		}	
	else if (`i' == 10) {	
		gen fuel_collection_minutes_`i' = .
		replace fuel_collection_minutes_`i' = (fuel_collection_time_`i' / 7) * 60 // Hours per week converted to minutes per day		
		}
	else {
		gen fuel_collection_minutes_`i'n2 = .
		replace fuel_collection_minutes_`i'n2 = (fuel_collection_time_`i' / 7) * 60 // Hours per week converted to minutes per day		
		}
	}
* Drop temporary fuel-collection time and units variables
drop fuel_collection_time_* fuel_collection_units_*
* Recording fuel-expenditure to convert to numeric
foreach i in 01 02 03 04 05 06 07 08 09 10 11n2 {
	capture replace fuel_expenditure_`i' = "40" if fuel_expenditure_`i' == "40 Ltr"
	destring fuel_expenditure_`i', replace
}
* Recording fuel-expenditure units to convert to numeric
foreach i in 01 02 03 04 05 06 07 08 09 10 11n2 {
	capture replace fuel_expenditure_units_`i' = "1" if fuel_expenditure_units_`i' == "1, 2, 3" | fuel_expenditure_units_`i' == "1,2, 3"
	capture replace fuel_expenditure_units_`i' = "2" if fuel_expenditure_units_`i' == "2 Month"
	destring fuel_expenditure_units_`i', replace
	}
	
* Self-reported fuelwood use
clonevar fueluse_01 = e25a_8 // Fuelwood use incorrectly labeled
replace fueluse_01 = "1.25" if fueluse_01 == "1/4"
destring fueluse_01, replace
replace fueluse_01 = fueluse_01 / 7 if e25c_8 == 2  // converting weekly to daily 
replace fueluse_01 = fueluse_01 / 30 if e25c_8 == 3 // converting monthly to daily
label var fueluse_01 "Fuelwood usage per day, kg - self reported"
ren fueluse_01 fuelwood_used_weight_reported

* Weighed fuelood use
replace e25b2n_1 = "0.5" if e25b2n_1 == "1/2"
destring e25b2n_1, replace
gen fuelwood_used_weight =  e25b1n_1 - e25b2n_1 
label var fuelwood_used_weight "Amount of firewood used during the monitoring period, kgs - WEIGHED"

* Stove maintenance
// Problems using Greenway
gen problems_greenway = (!mi(f16n1) & f16n1 != "0")
lab var problems_greenway "HH experienced problem with Gway stove"
// Problems using G-Coil
gen problems_gcoil = (!mi(f16n3) & f16n3 != "0")
lab var problems_gcoil "HH experienced problem with Gcoil stove"
// Maintenance Greenway
gen maintenance_greenway = (!mi(f17n1) & f17n1 != 0)
lab var maintenance_greenway "HH received maintenance for Gway stove"
// Maintenance G-Coil
gen maintenance_gcoil = (!mi(f17n3) & f17n3 != "0")
lab var maintenance_gcoil "HH received mainteance for Gcoil stove"

* Generate surveyround variable
gen surveyround = 2
label var surveyround "survey round; 0 = baseline, 1 = follow-up 1, 2 = follow-up 2"

* Order and rename variables

keep hh_id gpname gp_code IDcompleted surveyround lostin2014 hhmissing  ///
	hh_member_* age_year_* age_month_* age_day_* household_head_* sex_* cooks_* primary_cook_* ///
	cough_or_cold_* stove_own_* number_stove_own_* stove_used_* stove_use_hours_* stove_use_minutes_* ///
	fuel_use_* fuelwood_used_weight fuelwood_used_weight_reported fuel_collection_* fuel_expenditure_* problems_* maintenance_* 	

order hh_id gpname gp_code IDcompleted surveyround lostin2014 hhmissing  ///
	hh_member_* age_year_* age_month_* age_day_* household_head_* sex_* cooks_* primary_cook_* ///
	cough_or_cold_* stove_own_* number_stove_own_* stove_used_* stove_use_hours_* stove_use_minutes_* ///
	fuel_use_* fuelwood_used_weight fuelwood_used_weight_reported fuel_collection_* fuel_expenditure_* problems_* maintenance_*	

foreach var of varlist stove_own* number_stove_own* stove_used_* stove_use_* {
	ren `var' R2_`var'
	}

* SAVE: ROUND 2 ****************************************************************

save "${output}r2_followup_cleaned.dta", replace
